package cn.schoolwow.workflow.test.module.oa.common.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.deploy.DeployNodeType;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDeployFileContext;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDeployNode;
import cn.schoolwow.workflow.test.module.oa.common.flow.common.ConvertWorkFlowDeployJumpCommonFlow;
import cn.schoolwow.workflow.test.module.oa.common.flow.step.LoopConvertChildNodeCommonFlow;
import com.alibaba.fastjson.JSONObject;

public class ConvertToWorkFlowDeployFileContextFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        JSONObject workflow = flowContext.checkData("workflow", JSONObject.class);

        WorkFlowDeployFileContext workFlowDeployFileContext = new WorkFlowDeployFileContext();
        flowContext.putTemporaryInstanceData(workFlowDeployFileContext);

        //添加结束节点
        WorkFlowDeployNode endNode = new WorkFlowDeployNode();
        endNode.name = "结束";
        endNode.type = DeployNodeType.EndNode.value;
        workFlowDeployFileContext.nodes.add(endNode);
        flowContext.putTemporaryData("globalEndNode", endNode);

        flowContext.startFlow(new LoopConvertChildNodeCommonFlow())
                .putCurrentCompositeFlowData("topNode", workflow)
                .execute();

        JSONObject currentChildNode = flowContext.checkData("currentChildNode", JSONObject.class);
        //添加结束节点
        flowContext.startFlow(new ConvertWorkFlowDeployJumpCommonFlow())
                .putCurrentCompositeFlowData("currentChildNode", currentChildNode)
                .putCurrentCompositeFlowData("nextNode", endNode)
                .execute();
    }

    @Override
    public String name() {
        return "转换原始数据为部署文件上下文对象";
    }

}
